package io.jee.runshan.controller.admin;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.medusa.remote.RemoteConnector;
import io.jee.runshan.domain.Reply;
import io.jee.runshan.domain.VoiceAsk;
import io.jee.runshan.domain.remote.weixin.WeixinResponse;
import io.jee.runshan.service.IReplyService;
import io.jee.runshan.service.IVoiceAskService;
import io.jee.runshan.utils.Utils;
import io.jee.runshan.utils.WeixinConstant;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 语音消息Controller
 * @author SunX
 *
 */
@Controller
@RequestMapping("/admin/voiceAsk/")
public class VoiceAskController {
	
	private static final String LIST = "admin/voiceAsk/list";

	@Resource
	private IVoiceAskService voiceAskService;
	
	@Resource
	private IReplyService replyService;
	
	/**
	 * 语音列表
	 * 
	 * @param pager
	 * @param voiceAsk
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list(Pager<VoiceAsk> pager, VoiceAsk voiceAsk,
			@RequestParam(value = "replyStatus", required = true, defaultValue = "-1") Integer replyStatus,
			@RequestParam(value = "type", required = true, defaultValue = "-1") Integer type,
			@RequestParam(value = "readStatus", required = true, defaultValue = "-1") Integer readStatus) {
		ConditionGroup group = new ConditionGroup();
		if (!Utils.isNullOrEmpty(voiceAsk.getWxNikeName())) {
			group.and(Condition.onText("wxNikeName").like("%" + voiceAsk.getWxNikeName() + "%"));
		}
		if (replyStatus > -1) {
			group.and(Condition.onNumber("replyState").eq(replyStatus));
		}
		if (type > -1) {
			group.and(Condition.onNumber("type").eq(type));
		}
		if (readStatus > -1) {
			group.and(Condition.onNumber("isRead").eq(readStatus));
		}
		pager = voiceAskService.list(pager, group, "", "isRead ASC, publishTime DESC");
		return new ModelAndView(LIST).addObject("pager", pager)
				.addObject("voiceAsk", voiceAsk)
				.addObject("replyStatus", replyStatus)
				.addObject("type", type)
				.addObject("readStatus", readStatus);
	}
	
	/**
	 * 保存回复
	 * @param reply
	 * @return
	 */
	@RequestMapping(value = "/ajaxSaveReply", produces = { "application/json" })
	@ResponseBody
	public Map<String, Object> ajaxSaveReply(Integer voiceId, Reply reply) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			VoiceAsk voiceAsk = voiceAskService.get(voiceId);
			reply.setVoiceAsk(voiceAsk);
			reply.setReplyTime(new Date());
			
			String action = "message/custom/send";
			String sendUrl = WeixinConstant.sendUrl(action);
			String json = "{\"touser\":\"" + voiceAsk.getWxOpenid() + "\", \"msgtype\":\"text\", \"text\": { \"content\":\"" + reply.getReplyContent() + "\" } }";
			WeixinResponse response = RemoteConnector.post(sendUrl, json, WeixinResponse.class);
			if(response.getErrcode() == 0){//当请求成功在插入数据到数据库
				replyService.save(reply);
				voiceAskService.update("replyState", 1, voiceId);
				map.put("success", true);
			}
			map.put("msg", response.message());
		} catch (BeansException e) {
			e.printStackTrace();
			map.put("msg", "未知错误：" + e.getMessage());
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 获取回复列表
	 * @param voiceId
	 * @return
	 */
	@RequestMapping(value = "/ajaxReplyList", produces = { "application/json" })
	@ResponseBody
	public Map<String, Object> ajaxReplyList(Integer voiceId) {
		Map<String, Object> map = new HashMap<String, Object>();
		ConditionGroup group = new ConditionGroup();
		if (!Utils.isNullOrEmpty(voiceId)) {
			group.and(Condition.onNumber("voiceAsk.id").eq(voiceId));
		}
		List<Reply> list = replyService.list(group);
		List<Reply> volist = new ArrayList<Reply>();
		for (Reply reply : list) {
			Reply vo = new Reply(); 
			BeanUtils.copyProperties(reply, vo);
			vo.setVoiceAsk(null);
			volist.add(vo);
		}
		map.put("list", volist);
		return map;
	}
	
	
	@RequestMapping(value = "/ajaxRead", produces = { "application/json" })
	public void ajaxRead(Integer id) {
		VoiceAsk voice = voiceAskService.get(id);
		if (voice == null) {
			return;
		}
		voice.setIsRead(1);
		voiceAskService.update(voice);
		
	}
}
